﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.OpenGL;
using System.IO;
using System.Reflection;
using Silvermoon.Shapes;
using System.Drawing;
using Silvermoon.OpenGL.Native;

namespace Silvermoon.Images
{
    public static class SysImages
    {
        //private static Texture picker;
        private static Texture dot;
        private static Texture enter, forward, backward, shift, backspace, overflowDots;
        private static Texture edit;
        private static Texture trackbarFill;
        private static Texture radioBackground, radioCheck;
        private static Texture dialogBorder;
        //private static Texture rectBorder;
        private static Texture grabber, recycleBin;
        private static Texture notRounded, leftRounded, rightRounded, rounded;
        private static Texture bubbleArrow;
        private static Texture textboxGrabber;
        private static Texture roundButton;
        private static Texture dialog;
        private static Texture circle;
        //private static Texture back;
        private static Texture alphaAccent, accentLum;
        private static Texture accent;
        private static Texture progressBorder;
        private static Texture keyboard;
        private static Texture buttonBackground;
        private static Texture itemBackground;
        private static Texture textBox, textBoxChrome;
        private static Texture rectangle;
        private static Texture gripper;
        private static Texture subItemArrow;
        private static Texture check;
        private static Texture point;
        private static Texture busy;
        private static Texture cut, copy, paste;
        private static Texture select;
        private static Texture trackbarRange;
        private static Texture trackbarButton;
        private static Texture find;
        private static Texture ok, cancel, add;


        public static Texture Dot
        {
            get
            {
                if (dot == null) dot = CreateImage("dot", TextureFormat.LuminanceAlpha, false);
                return dot;
            }
        }

        internal static Texture Backspace
        {
            get
            {
                if (backspace == null) backspace = CreateImage("Backspace", TextureFormat.LuminanceAlpha, true);
                return backspace;
            }
        }


        internal static Texture OverflowDots
        {
            get
            {
                if (overflowDots == null) overflowDots = CreateImage("overflowdots", TextureFormat.LuminanceAlpha, true);
                return overflowDots;
            }
        }

        public static Texture Shift
        {
            get
            {
                if (shift == null) shift = CreateImage("Shift", TextureFormat.LuminanceAlpha, true);
                return shift;
            }
        }


        public static Texture Forward
        {
            get
            {
                if (forward == null) forward = CreateImage("Forward", TextureFormat.LuminanceAlpha, true);
                return forward;
            }
        }

        public static Texture Backward
        {
            get
            {
                if (backward == null) backward = CreateImage("Backward", TextureFormat.LuminanceAlpha, true);
                return backward;
            }
        }



        internal static Texture Enter
        {
            get
            {
                if (enter == null) enter = CreateImage("Enter", TextureFormat.LuminanceAlpha, true);
                return enter;
            }
        }

        public static Texture Add
        {
            get
            {
                if (add == null) add = CreateImage("add", TextureFormat.LuminanceAlpha, true);
                return add;
            }
        }


        public static Texture Ok
        {
            get
            {
                if (ok == null) ok = CreateImage("ok", TextureFormat.LuminanceAlpha, true);
                return ok;
            }
        }

        public static Texture Cancel
        {
            get
            {
                if (cancel == null) cancel = CreateImage("cancel", TextureFormat.LuminanceAlpha, true);
                return cancel;
            }
        }


        public static Texture Find
        {
            get
            {
                if (find == null) find = CreateImage("Find", TextureFormat.LuminanceAlpha, true);
                return find;
            }
        }


        public static Texture Edit
        {
            get
            {
                if (edit == null) edit = CreateImage("Edit", TextureFormat.LuminanceAlpha, true);
                return edit;
            }
        }

        public static Texture TrackbarFill
        {
            get
            {
                if (trackbarFill == null) trackbarFill = CreateImage("TrackbarFill", TextureFormat.LuminanceAlpha, true);
                return trackbarFill;
            }
        }


        public static Texture RadioBackground
        {
            get
            {
                if (radioBackground == null) radioBackground = CreateImage("Radio", TextureFormat.LuminanceAlpha, true);
                return radioBackground;
            }
        }

        public static Texture RadioCheck
        {
            get
            {
                if (radioCheck == null) radioCheck = CreateImage("RadioCheck", TextureFormat.LuminanceAlpha, false);
                return radioCheck;
            }
        }

        public static Texture DialogBorder
        {
            get
            {
                if (dialogBorder == null) dialogBorder = CreateImage("DialogBorder", TextureFormat.LuminanceAlpha, false);
                return dialogBorder;
            }
        }

        public static Texture RecycleBin
        {
            get
            {
                if (recycleBin == null) recycleBin = CreateImage("RecycleBin", TextureFormat.LuminanceAlpha, false);
                return recycleBin;
            }
        }

        public static Texture Grabber
        {
            get
            {
                if (grabber == null) grabber = CreateImage("Grabber", TextureFormat.LuminanceAlpha, true);
                return grabber;
            }
        }

        public static Texture RightRounded
        {
            get
            {
                if (rightRounded == null) rightRounded = CreateImage("RightRounded", TextureFormat.LuminanceAlpha, true);
                return rightRounded;
            }
        }

        public static Texture NotRounded
        {
            get
            {
                if (notRounded == null) notRounded = CreateImage("NotRounded", TextureFormat.LuminanceAlpha, true);
                return notRounded;
            }
        }

        public static Texture Rounded
        {
            get
            {
                if (rounded == null) rounded = CreateImage("Rounded", TextureFormat.LuminanceAlpha, true);
                return rounded;
            }
        }

        public static Texture LeftRounded
        {
            get
            {
                if (leftRounded == null) leftRounded = CreateImage("LeftRounded", TextureFormat.LuminanceAlpha, true);
                return leftRounded;
            }
        }

        public static Texture BubbleArrow
        {
            get
            {
                if (bubbleArrow == null) bubbleArrow = CreateImage("BubbleArrow", TextureFormat.LuminanceAlpha, true);
                return bubbleArrow;
            }
        }

        public static Texture TextboxGrabber
        {
            get
            {
                if (textboxGrabber == null) textboxGrabber = CreateImage("TextboxGrabber", TextureFormat.LuminanceAlpha, true);
                return textboxGrabber;
            }
        }

        public static Texture TrackbarRange
        {
            get
            {
                if (trackbarRange == null) trackbarRange = CreateImage("TrackbarRange", TextureFormat.LuminanceAlpha, true);
                return trackbarRange;
            }
        }

        public static Texture TrackbarButton
        {
            get
            {
                if (trackbarButton == null) trackbarButton = CreateImage("TrackbarButton", TextureFormat.RGBA5551, true);
                return trackbarButton;
            }
        }

        public static Texture Select
        {
            get
            {
                if (select == null) select = CreateImage("Select", TextureFormat.LuminanceAlpha, false);
                return select;
            }
        }

        public static Texture Back
        {
            get { return Backward; }
        }



        public static Texture AlphaAccent
        {
            get
            {
                if (alphaAccent == null)
                {
                    alphaAccent = CreateImage("Accent", TextureFormat.HiResAlpha, true);
                }
                return alphaAccent;
            }
        }

        public static Texture AccentLuminance
        {
            get
            {
                if (accentLum == null)
                {
                    accentLum = CreateImage("Accent", TextureFormat.Luminance, true);
                }
                return accentLum;
            }
        }

        public static Texture Accent
        {
            get
            {
                if (accent == null)
                {
                    accent = CreateImage("Accent", TextureFormat.RGBA5551, true);
                }
                return accent;
            }
        }

        public static Texture ProgressBorder
        {
            get
            {
                if (progressBorder == null) progressBorder = CreateImage("ProgressBorder", true);
                return progressBorder;
            }
        }


        /// <summary>
        /// Gets the 32x32 image that contains the 4 quadrants to be used with a <see cref="T:FourQuadrantShape"/>.
        /// </summary>
        /// <value>The text box.</value>
        public static Texture TextBox
        {
            get
            {
                if (textBox == null)
                {
                    textBox = CreateImage("TextBox");
                    textBox.IsSigned = true;
                }
                return textBox;
            }
        }

        /// <summary>
        /// Gets the 32x32 image that contains the 4 quadrants to be used with a <see cref="T:FourQuadrantShape"/>.
        /// </summary>
        /// <value>The text box.</value>
        public static Texture TextBoxChrome
        {
            get
            {
                if (textBoxChrome == null) textBoxChrome = CreateImage("TextBoxChrome");
                return textBoxChrome;
            }
        }

        /// <summary>
        /// Gets the 16x16 image that contains the 4 quadrants to be used with a <see cref="T:FourQuadrantShape"/>.
        /// </summary>
        /// <value>The text box.</value>
        public static Texture Gripper
        {
            get
            {
                if (gripper == null) gripper = CreateImage("Gripper", TextureFormat.LuminanceAlpha, true);
                return gripper;
            }
        }

        /// <summary>
        /// Gets the 16x16 image that contains the 4 quadrants to be used with a <see cref="T:FourQuadrantShape"/>.
        /// </summary>
        /// <value>The text box.</value>
        public static Texture Rectangle
        {
            get
            {
                if (rectangle == null) rectangle = CreateImage("Rectangle");
                return rectangle;
            }
        }

        /// <summary>
        /// Gets the 32x32 image that contains the 4 quadrants to be used with a <see cref="T:FourQuadrantShape"/>.
        /// </summary>
        public static Texture Button
        {
            get
            {
                //if (buttonBackground == null) buttonBackground = CreateImage("Button", true);
                if (buttonBackground == null)
                {
                    buttonBackground = CreateImage("Chrome2", TextureFormat.LuminanceAlpha);
                    buttonBackground.IsSigned = true;
                }
                return buttonBackground;
            }
        }

        public static Texture RoundButton
        {
            get
            {
                if (roundButton == null)
                {
                    roundButton = CreateImage("RoundButton", TextureFormat.RGBA5551);
                    roundButton.IsSigned = true;
                }
                return roundButton;
            }
        }

        public static Texture ItemBackground
        {
            get
            {
                if (itemBackground == null) itemBackground = CreateImage("ItemBackground", true);
                return itemBackground;
            }
        }

        public static Texture Keyboard
        {
            get
            {
                if (keyboard == null)
                {
                    keyboard = CreateImage("Keyboard");
                    //keyboard.Size = new Size(48, 48);
                }
                return keyboard;
            }
        }


        public static Texture Check
        {
            get
            {
                if (check == null) check = CreateImage("Check", TextureFormat.LuminanceAlpha);
                return check;
            }


        }

        public static Texture Point
        {
            get
            {
                if (point == null) point = CreateImage("Point", true);
                return point;
            }
        }

        public static Texture Circle
        {
            get
            {
                if (circle == null)
                {
                    circle = CreateImage("Circle", TextureFormat.LuminanceAlpha);
                }
                return circle;
            }
        }

        public static Texture Dialog
        {
            get
            {
                if (dialog == null)
                {
                    dialog = CreateImage("Dialog2", TextureFormat.RGBA5551);
                    dialog.IsSigned = true;
                }
                return dialog;
            }
        }

        public static Texture Copy
        {
            get
            {
                if (copy == null)
                {
                    copy = CreateImage("Copy", TextureFormat.LuminanceAlpha);
                }
                return copy;
            }
        }

        public static Texture Cut
        {
            get
            {
                if (cut == null)
                {
                    cut = CreateImage("Cut", TextureFormat.LuminanceAlpha);
                }

                return cut;
            }
        }

        public static Texture Paste
        {
            get
            {
                if (paste == null)
                {
                    paste = CreateImage("Paste", TextureFormat.LuminanceAlpha);
                    //   paste.IsSigned = true;
                }
                return paste;
            }
        }

        public static Texture Busy
        {
            get
            {
                if (busy == null)
                {
                    busy = CreateImage("Busy", TextureFormat.LuminanceAlpha, true);
                }
                return busy;
            }
        }

        public static Texture SubItemArrow
        {
            get
            {
                if (subItemArrow == null) subItemArrow = CreateImage("Arrow", true);
                return subItemArrow;
            }
        }

        private static Texture CreateImage(string name)
        {
            return CreateImage(name, TextureFormat.Automatic);
        }

        private static Texture CreateImage(string name, bool translucent)
        {
            return CreateImage(name, translucent ? TextureFormat.RGBA4444 : TextureFormat.Automatic);
        }

        private static Texture CreateImage(string name, TextureFormat format)
        {
            string resource = string.Format("Silvermoon.Images.{0}.png", name);
            Stream stream = Assembly.GetCallingAssembly().GetManifestResourceStream(resource);
            if (stream == null) throw new SystemException("Resource " + resource + " not found.");
            Texture image = Texture.FromStream(stream, format);

            return image;
        }

        private static Texture CreateImage(string name, TextureFormat format, bool isSigned)
        {
            Texture image = CreateImage(name, format);
            image.IsSigned = isSigned;
            return image;
        }

    }
}
